/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EntertainmentGuideGui.java
 *
 * Created on 4 Φεβ 2012, 2:54:27 μμ
 */

package gui;

import HelperClasses.Messages.TerminateQuestion;
import HelperClasses.UserType;
import entertainmentguide.entity.User;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import manageDataBase.DBManager;
/**
 *
 *
 */
public class EntertainmentGuideGui extends javax.swing.JFrame {
    
    // ο τρέχων χρήστης
    static public  User loggedUser = new User(); 
    
    /** Creates new form EntertainmentGuideGui */
    public EntertainmentGuideGui() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LogoLabel = new javax.swing.JLabel();
        TitleLabel = new javax.swing.JLabel();
        UserNameLabel = new javax.swing.JLabel();
        UserNameTextField = new javax.swing.JTextField();
        UserPswLabel = new javax.swing.JLabel();
        UserPswPasswordField = new javax.swing.JPasswordField();
        jButtonConnect = new javax.swing.JButton();
        jLabelPasswordNotFound = new javax.swing.JLabel();
        jLabelUserNotFound = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Restaurant Guide");
        setMinimumSize(new java.awt.Dimension(800, 800));
        setPreferredSize(new java.awt.Dimension(800, 800));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowDeactivated(java.awt.event.WindowEvent evt) {
                formWindowDeactivated(evt);
            }
        });
        getContentPane().setLayout(null);

        LogoLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/thessaloniki-restaurants.jpg"))); // NOI18N
        getContentPane().add(LogoLabel);
        LogoLabel.setBounds(0, 0, 400, 120);

        TitleLabel.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        TitleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        TitleLabel.setText("Σύνδεση στην εφαρμογή");
        TitleLabel.setPreferredSize(new java.awt.Dimension(310, 30));
        getContentPane().add(TitleLabel);
        TitleLabel.setBounds(410, 50, 310, 30);

        UserNameLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        UserNameLabel.setText("Όνομα Χρήστη :");
        UserNameLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        UserNameLabel.setMaximumSize(new java.awt.Dimension(90, 14));
        UserNameLabel.setMinimumSize(new java.awt.Dimension(90, 14));
        UserNameLabel.setPreferredSize(new java.awt.Dimension(90, 14));
        getContentPane().add(UserNameLabel);
        UserNameLabel.setBounds(210, 283, 90, 14);

        UserNameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UserNameTextFieldActionPerformed(evt);
            }
        });
        UserNameTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                UserNameTextFieldFocusGained(evt);
            }
        });
        getContentPane().add(UserNameTextField);
        UserNameTextField.setBounds(314, 280, 240, 20);

        UserPswLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        UserPswLabel.setText("Κωδικός :");
        UserPswLabel.setMaximumSize(new java.awt.Dimension(90, 14));
        UserPswLabel.setMinimumSize(new java.awt.Dimension(90, 14));
        UserPswLabel.setPreferredSize(new java.awt.Dimension(90, 14));
        getContentPane().add(UserPswLabel);
        UserPswLabel.setBounds(210, 306, 90, 14);

        UserPswPasswordField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                UserPswPasswordFieldFocusGained(evt);
            }
        });
        getContentPane().add(UserPswPasswordField);
        UserPswPasswordField.setBounds(314, 306, 240, 20);

        jButtonConnect.setText("Σύνδεση");
        jButtonConnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConnectActionPerformed(evt);
            }
        });
        getContentPane().add(jButtonConnect);
        jButtonConnect.setBounds(380, 423, 110, 50);

        jLabelPasswordNotFound.setForeground(new java.awt.Color(255, 0, 0));
        jLabelPasswordNotFound.setText("O Κωδικός δεν βρέθηκε");
        jLabelPasswordNotFound.setMaximumSize(new java.awt.Dimension(170, 14));
        jLabelPasswordNotFound.setMinimumSize(new java.awt.Dimension(170, 14));
        jLabelPasswordNotFound.setPreferredSize(new java.awt.Dimension(170, 14));
        getContentPane().add(jLabelPasswordNotFound);
        jLabelPasswordNotFound.setBounds(560, 309, 170, 14);

        jLabelUserNotFound.setForeground(new java.awt.Color(255, 0, 0));
        jLabelUserNotFound.setText("O Χρήστης δεν βρέθηκε");
        jLabelUserNotFound.setMaximumSize(new java.awt.Dimension(170, 14));
        jLabelUserNotFound.setMinimumSize(new java.awt.Dimension(170, 14));
        jLabelUserNotFound.setPreferredSize(new java.awt.Dimension(170, 14));
        getContentPane().add(jLabelUserNotFound);
        jLabelUserNotFound.setBounds(560, 284, 170, 14);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void UserNameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UserNameTextFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_UserNameTextFieldActionPerformed

    private void jButtonConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConnectActionPerformed
        
        DBManager db = new DBManager();
        
        String fUserName=UserNameTextField.getText().trim();
        String fPassword= new String(UserPswPasswordField.getPassword());
        
        if (!fUserName.isEmpty()){ // δόθηκε username από τον χρήστη
            try {
                User cUser =  db.getUserByName(fUserName); // βρες τον χρήστη
                if (cUser==null){
                    // δεν βρέθηκε χρήστης
                    jLabelUserNotFound.setVisible(true);
                }
                else  // User βρέθηκε
                {
                    if (cUser.getPassword().equals(fPassword)){
                        //  βρέθηκε password - βρέθηκε user
                        if (cUser.getStatus()==UserType.ADMINISTRATOR.toInt()){
                            // χρήστης είναι administrator
                            this.loggedUser=cUser;
                            JFrame AdminMainForm = new AdministratorMainGui();
                            AdministratorMainGui.loggedUser = this.loggedUser;
                            AdminMainForm.setBounds(50, 100, 800, 800);
                            AdminMainForm.setLocationRelativeTo(null); // possition center of screen
                            AdminMainForm.setResizable(false); // not resizeable
                            AdminMainForm.setVisible(true); // show AdministratorMainGui 
                            dispose();      
                        }
                        if (cUser.getStatus()==UserType.USER.toInt()){
                            // χρήστης είναι απλός χρήστης
                            this.loggedUser=cUser;
                            this.setVisible(false);
                            JFrame UserMainForm = new UserMainGui();
                            UserMainGui.loggedUser = this.loggedUser;
                            UserMainForm.setBounds(50, 100, 800, 800);
                            UserMainForm.setLocationRelativeTo(null); // possition center of screen
                            UserMainForm.setResizable(false); // not resizeable
                            UserMainForm.setVisible(true); // show UserMainGui 
                            dispose();
                        }
                    }
                    else{ // δεν βρέθηκε password - βρέθηκε user
                        jLabelPasswordNotFound.setVisible(true);
                    }
                }
            } catch (SQLException ex) {
                Logger.getLogger(EntertainmentGuideGui.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else{ // δεν δόθηκε username από τον χρήστη 
            jLabelUserNotFound.setVisible(true);
        }
        try {
            db.closeDB();
        } catch (SQLException ex) {
            Logger.getLogger(EntertainmentGuideGui.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButtonConnectActionPerformed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        // κατά την ενεργοποίηση της φόρμας απενεργοποιώ τα μηνύματα λάθους
        jLabelUserNotFound.setVisible(false);
        jLabelPasswordNotFound.setVisible(false);
        this.rootPane.setDefaultButton(jButtonConnect);
    }//GEN-LAST:event_formWindowActivated

    private void UserNameTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_UserNameTextFieldFocusGained
        // απενεργοποιώ το μήνυμα λάθους για τον username
        jLabelUserNotFound.setVisible(false);
    }//GEN-LAST:event_UserNameTextFieldFocusGained

    private void UserPswPasswordFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_UserPswPasswordFieldFocusGained
        // ενεργοποιώ το μήνυμα λάθους για τον username
        jLabelPasswordNotFound.setVisible(false);
    }//GEN-LAST:event_UserPswPasswordFieldFocusGained

    private void formWindowDeactivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowDeactivated
    // έξοδος από την φόρμα και από το πρόγραμμα
       
    }//GEN-LAST:event_formWindowDeactivated

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        TerminateQuestion.windowClosing(this);
       
    }//GEN-LAST:event_formWindowClosing

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new EntertainmentGuideGui().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel LogoLabel;
    private javax.swing.JLabel TitleLabel;
    private javax.swing.JLabel UserNameLabel;
    private javax.swing.JTextField UserNameTextField;
    private javax.swing.JLabel UserPswLabel;
    private javax.swing.JPasswordField UserPswPasswordField;
    private javax.swing.JButton jButtonConnect;
    private javax.swing.JLabel jLabelPasswordNotFound;
    private javax.swing.JLabel jLabelUserNotFound;
    // End of variables declaration//GEN-END:variables

}
